#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"
  kubectl create secret generic -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-minio" \
     --from-literal=accesskey=test --from-literal=secretkey=test

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 1 \
    --set-string configurations.objectstorage.s3Compatible.bucket=test \
    --set-string configurations.objectstorage.s3Compatible.awsCredentials.secretKeySelectors.accessKeyId.name="$CLUSTER_NAME-minio" \
    --set-string configurations.objectstorage.s3Compatible.awsCredentials.secretKeySelectors.accessKeyId.key=accesskey \
    --set-string configurations.objectstorage.s3Compatible.awsCredentials.secretKeySelectors.secretAccessKey.name="$CLUSTER_NAME-minio" \
    --set-string configurations.objectstorage.s3Compatible.awsCredentials.secretKeySelectors.secretAccessKey.key=secretkey \
    --set-string "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE"
}

e2e_test() {
  run_test "Trying to delete objectstorage with cluster created" delete_with_cluster

  run_test "Trying to delete objectstorage without cluster created" delete_whitout_cluster
}

delete_with_cluster() {
  if ERROR="$(kubectl delete -n "$CLUSTER_NAMESPACE" --dry-run=server sgobjectstorages.stackgres.io backupconf 2>&1)"
  then
    success "Good. It has deleted the configuration"
  else
    fail "It should be able to delete the posgresconf. ERROR ${ERROR}"
  fi
}

delete_whitout_cluster() {
  kubectl get -n "$CLUSTER_NAMESPACE" sgclusters.stackgres.io "$CLUSTER_NAME" -o yaml | kubectl delete -f -

  if ERROR="$(kubectl delete -n "$CLUSTER_NAMESPACE" --dry-run=server sgobjectstorages.stackgres.io backupconf 2>&1)"
  then
    success "Good. It has deleted the configuration"
  else
    fail "It should be able to delete the posgresconf. ERROR ${ERROR}"
  fi
}
